package com.desmart.renjiao.web;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.desmart.renjiao.R;
import com.lemon.library.common.router.Contanst;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonActivity;
import com.lemon.library.constants.PreferencesConstants;
import com.lemon.library.utils.PreferencesUtils;

@Route(path = ARoutePath.Web.WebViewActivity)
public class WebViewActivity extends CommonActivity {

    private WebView webView;
    private WebSettings webSettings;
    private ProgressBar progressBar;

    @Override
    public int bindLayout() {
        return R.layout.activity_webview;
    }

    @Override
    public void initView(Activity activity) {
        showCloseButton();
        progressBar = activity.findViewById(R.id.prgressbar);
        webView = activity.findViewById(R.id.webview);

        webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setSupportZoom(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                String token = PreferencesUtils.getString(PreferencesConstants.login_Token);
                setCookie(url,"LtpaToken=" + token);
                super.onPageStarted(view, url, favicon);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress < 100) {
                    progressBar.setProgress(newProgress);
                    progressBar.setVisibility(View.VISIBLE);
                } else {
                    progressBar.setVisibility(View.GONE);
                }
            }

        });
    }

    @Override
    public void initData(Context context) {

        String url = getIntent().getStringExtra(Contanst.webview.Key_Url);
        String title = getIntent().getStringExtra(Contanst.webview.Key_Title);
        setTitleText(title);

        String token = PreferencesUtils.getString(PreferencesConstants.login_Token);
        setCookie(url,"LtpaToken=" + token);
        webView.loadUrl(url);

    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack())
            webView.goBack();
        else {
            super.onBackPressed();
        }
    }

    private void setCookie(String url,String cookieContent){
        CookieManager cm = CookieManager.getInstance();
        CookieSyncManager csm = CookieSyncManager.createInstance(this);
        cm.setAcceptCookie(true);
        cm.setCookie(url, cookieContent);

        if(Build.VERSION.SDK_INT >Build.VERSION_CODES.LOLLIPOP){
            cm.flush();
        }else {
            csm.sync();
        }
    }

}
